package com.zxy.demo.java.reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.alibaba.fastjson.JSON;
import com.google.common.reflect.Reflection;

import static com.google.common.reflect.Reflection.newProxy;

/**
 * Person.
 * 
 * Created by zxybird on 2017/12/7.
 */
public class Person implements IPerson {

  private String name;

  private Integer age;

  private Integer sex;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder("Person{");
    sb.append("name='").append(getName()).append('\'');
    sb.append(", age=").append(getAge());
    sb.append('}');
    return sb.toString();
  }

  public static void main(String[] args) throws NoSuchMethodException {

    IPerson person = new Person();

    person.setName("zxy");
    person.setAge(18);

    Method getAgeMethod = Person.class.getMethod("getAge");
    getAgeMethod.setAccessible(false);
    // getAgeMethod.
    // person = Reflection.newProxy(Person.class, null);

    person = Reflection.newProxy(IPerson.class, new InvocationHandler() {
      private IPerson object;

      public InvocationHandler setObject(IPerson object) {
        this.object = object;
        return this;
      }

      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("getAge".equals(method.getName())) {
          return null;
        }

        return method.invoke(object, args);
      }
    }.setObject(person));

    // Reflection.registerMethodsToFilter(Person.class);

    System.out.println(JSON.toJSONString(person));

  }

}
